/**
 * 渠道折扣系数编辑
 * @author linfeng
 * @since 2020/08/31 16:06
 */
layui.define(['form', 'layedit', 'laydate'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let index;

    let laydate = layui.laydate;

    let effectiveDateObj = {
        elem: '#effectiveDate',
        type: 'date',
        btns: ['clear', 'confirm'],
        min: 1
    };

    laydate.render(effectiveDateObj);

    form.render(null, "zmn-cityratio-form");

    zmnLayUtil.layFormVerify(form);

    Date.prototype.format = function(fmt) {
        var o = {
            "M+" : this.getMonth()+1,                 //月份
            "d+" : this.getDate(),                    //日
            "h+" : this.getHours(),                   //小时
            "m+" : this.getMinutes(),                 //分
            "s+" : this.getSeconds(),                 //秒
            "q+" : Math.floor((this.getMonth()+3)/3), //季度
            "S"  : this.getMilliseconds()             //毫秒
        };
        if(/(y+)/.test(fmt)) {
            fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o) {
            if(new RegExp("("+ k +")").test(fmt)){
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
            }
        }
        return fmt;
    }

    // 自定义验证规则
    form.verify({
        range: function(value){
            if (value != null && value != undefined && value != ""){
                if (value == '0' || value == '0.0' || value == '0.00'){
                    return "系数值不符合规定,系数值范围0.6~3.0";
                }
                if(value < 0.6 || value > 3){
                    return "系数值不符合规定,系数值范围0.6~3.0";
                }
                var reg = new RegExp("^[0-9]+.?[0-9]{0,2}$");
                if(!reg.test(value)){
                    return "请输入正确的格式!#.##";
                }
            }
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        if (obj.field.effectiveDate == null || obj.field.effectiveDate == '' || obj.field.effectiveDate == undefined) {
            var nowDate = new Date();
            let effectiveDateStr = new Date(nowDate.getTime() + 24*60*60*1000).format("yyyy-MM-dd");
            obj.field.effectiveDate = effectiveDateStr;
        } else {
            obj.field.effectiveDate = obj.field.effectiveDate;
        }

        let toBeEffectiveRatio = obj.field.toBeEffectiveRatio;

        layer.confirm('渠道折扣系数确认修改为"'+toBeEffectiveRatio+'"吗？', function () {
            var index;
            var url = "/channel/addEditDiscount.action";
            $.ajax({
                async: false,
                type: "POST", url: url,
                contentType: "application/json", dataType: "json", data: JSON.stringify(obj.field),
                beforeSend: function () {
                    index = layer.load(1, {
                        shade: [0.5, '#DBDBDB']
                    });
                },
                success:function(resp) {
                    layer.close(index);
                    if(resp.status == 200) {
                        zmnLayUtil.closeThisIframeMsg(parent, resp.message, "zmn-table");
                        parent.location.reload();
                    } else {
                        layer.msg(resp.message, {"icon": 2});
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    layer.close(index);
                }
            })
        });

        return false;
    });

    //对外暴露的接口
    exports('channel_price_discount_edit', {});
});

function cancel() {
    let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
}

/**
 * 验证输入的值的范围为0.6~3.0最多两位小数 0代表不设置
 * @param value
 * @returns {boolean}
 */
function valueVerify(value) {
    var reg=/^\d+(\.\d{1,2})?$/;

    if (value == ""){
        return true;
    }

    if (isNaN(value)){
        layer.alert("请输入正确的数字");
        return false;
    }

    // if (value == '0' || value == '0.0' || value == '0.00'){
    //     return true;
    // }

    if(!reg.test(value))
    {
        layer.alert("请输入整数或者1到2位小数");
        return false;
    }

    if (parseFloat(0.6) <= parseFloat(value) && parseFloat(3.0) >= parseFloat(value)){
    } else{
        layer.alert("系数值不符合规定,系数值范围0.6~3.0");
        return false;
    }

    return true;
}
